﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Graphic = Ribbon.Core.Graphics.GraphicHelper;

namespace Ribbon.Core
{
    public class RibbonPage : RibbonPageBase
    {
        public RibbonPage(string name)
        {
            Groups = new RibbonGroupCollection(this);
            this.Name = name;
            this.Text = name;
            this.Rect = null;
            this.Radiu = 1;
        }

        public RibbonPage(string name, string text)
            : this(name)
        {
            this.Text = text;
        }


        /// <summary>
        /// this componet's son
        /// </summary>
        private RibbonGroupCollection groups;

        public RibbonGroupCollection Groups
        {
            get { return groups; }
            set { groups = value; }
        }

        Font font;

        public override Font Font
        {
            get
            {
                return font;
            }
            set
            {
                font = value;

                if (value != null)
                {
                    foreach (RibbonGroup item in this.groups)
                    {
                        if (item.Font == null)
                        {
                            item.Font = this.font;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// this componet's container
        /// </summary>
        private Ribbon parent;

        public override Ribbon Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;

                if (this.Font == null)
                    this.Font = this.Parent.Font;

                GetSize();

                if (this.groups != null)
                {
                    float width = 0;

                    for (int i = 0; i < this.groups.Count; i++)
                    {
                        this.groups[i].GroupRect = new Rect(width, this.Parent.TabHeight, 100, this.Parent.Height - this.Parent.TabHeight);

                        foreach (var item in this.groups[i].Items)
                        {
                            item.Resize();
                        }

                        width += this.groups[i].GroupRect.Width;
                    }
                }
            }
        }

        public override string ToString()
        {
            return string.Format("Name:{0} IsActive:{1}", this.Name, this.IsActive);
        }

        internal virtual void Refresh()
        {
            this.Parent.Refresh();
        }

        internal override void GetSize()
        {
            if (this.parent != null)
            {
                TextSize = Graphic.Graphics.MeasureString(this.Text, this.Font);

                int height = (int)(TextSize.Height + 10 * DPIHelper.DPIScale);

                if (this.parent.TabHeight < height)
                {
                    this.parent.TabHeight = height;
                }


                height = (int)(TextSize.Height + 2 * DPIHelper.DPIScale);

                float y = (this.parent.TabHeight - height) / 2f;

                if (this.Radiu > height)
                    this.Radiu = height;

                this.Rect = new Rect(this.parent.TempEmptyX, y, TextSize.Width + this.Radiu, height);

                if (this.Image != null)
                {
                    this.Rect.Width += 21f;
                }

                this.parent.TempEmptyX += this.Rect.Width + 2f;
            }
        }

        public override void OnPaint(System.Drawing.Graphics g)
        {
            if (this.Rect == null)
            {
                throw new Exception("none region");
            }

            Brush bru_Hearder = this.IsActive ? new SolidBrush(SelectForeColor) : new SolidBrush(ForeColor);

            if (this.IsActive || this.IsMouseHover)
            {
                Brush bru_HearderBack = this.IsActive ? new SolidBrush(SelectBackColor) : new SolidBrush(Color.FromArgb(this.BackColor.R - 20, this.BackColor.G - 20, this.BackColor.B - 20));

                if (this.Radiu > 1)
                {
                    //Draw Hover Tab Background
                    System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();

                    path.AddLine(this.Rect.X, this.Rect.Y + this.Rect.Height - this.Radiu, this.Rect.X, this.Rect.Y + this.Radiu);

                    path.AddArc(this.Rect.X, this.Rect.Y, this.Radiu, this.Radiu, 180, 90);

                    path.AddLine(this.Rect.X + this.Radiu, this.Rect.Y, this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y);

                    path.AddArc(this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y, this.Radiu, this.Radiu, 270, 90);

                    path.AddLine(this.Rect.X + this.Rect.Width, this.Rect.Y + this.Radiu, this.Rect.X + this.Rect.Width, this.Rect.Y + this.Rect.Height - this.Radiu);

                    path.AddArc(this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y + this.Rect.Height - this.Radiu, this.Radiu, this.Radiu, 0, 90);

                    path.AddLine(this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y + this.Rect.Height, this.Rect.X + this.Radiu, this.Rect.Y + this.Rect.Height);

                    path.AddArc(this.Rect.X, this.Rect.Y + this.Rect.Height - this.Radiu, this.Radiu, this.Radiu, 90, 90);

                    g.FillPath(bru_HearderBack, path);

                    using (path) { }
                }
                else
                {
                    g.FillRectangle(bru_HearderBack, this.Rect.X, this.Rect.Y, this.Rect.Width, this.Rect.Height);
                }


                if (this.IsActive)
                {
                    foreach (RibbonGroup group in this.Groups)
                    {
                        group.OnPaint(g);
                    }
                }

                using (bru_HearderBack) { }
            }

            g.DrawString(this.Text, Font, bru_Hearder, this.Rect.X + (this.Rect.Width - this.TextSize.Width) / 2 + 1f, this.Rect.Y + (this.Rect.Height - this.TextSize.Height) / 2 + 1f);

            using (bru_Hearder) { }
        }
    }


}
